﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.SystemManage.Controllers
{
    public class MemberController :MvcControllerBase
    {
        private MemberBLL bll = new MemberBLL();

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Form()
        {
            ViewBag.Mode = new CodeRuleBLL().CheckBillCodeMode(((int)CodeRuleEnum.Member).ToString());
            return View();
        }


        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = bll.GetEntity(keyValue);
            return ToJsonResult(data);
        }

        public ActionResult GetPStructLsit(string MemberId)
        {
            var dataRelation = bll.GetRelationWPList().Where(t => t.MemberId == MemberId).ToList();
            var dataPstruct = new PStructBLL().GetList().Where(t => t.Type == 5).ToList();
            var dataCustomer= new CustomerInfoBLL().GetList().ToList();
            var data = from a in dataRelation
                       join b in dataPstruct on a.PStructId equals b.Id
                       join c in dataCustomer on a.CustomerId equals c.Id
                       select new
                       {
                           a.Id,
                           a.PStructId,
                           a.MemberId,
                           a.CustomerId,
                           a.AccountCode,
                           a.IsDefault,
                           a.Type,
                           a.EnabledMark,
                           a.CreateDate,
                           a.ModifyDate,
                           PStructName = b.AllName,
                           CustomerName = c.Name
                       };

            return Content(data.ToJson());
        }
        

        [HttpGet]
        public ActionResult GetListJson(Pagination pagination,string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data = bll.GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult RemoveForm(string keyValue)
        {
            if (bll.GetRelationWPList().Where(t => t.MemberId == keyValue && t.EnabledMark==1).Count() > 0)
            {
                return Error("请先解除房间绑定。");
            }
            else
            {
                bll.RemoveForm(keyValue);
                return Success("删除成功。");
            }
         
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string member,string pstructlist)
        {
            MemberInfoEntity entity = member.ToObject<MemberInfoEntity>();
            List<RelationWPEntity> list = pstructlist.ToObject<List<RelationWPEntity>>();
            bll.SaveForm(keyValue, entity,list);
            return Success("操作成功。");
        }
    }
}
